package com.labelimage.controller;

import com.labelimage.bean.entity.LabelImageEntity;
import com.labelimage.bean.param.LabelImageParam;
import com.labelimage.bean.vo.LabelImageVo;
import com.labelimage.common.response.Result;
import com.labelimage.service.LabelImageService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("labelimage")
@Validated
public class LabelImageController {
    @Resource
    private LabelImageService labelImageService;

    @GetMapping("")
    public Result<List<LabelImageVo>> getRandom(@RequestParam("templateId") Long templateId,
                                                @RequestParam("num") Integer num,
                                                @RequestParam(value = "unLabelImageIds",required = false,defaultValue = "") List<Long> unLabelImageIds) {

        return Result.OK(labelImageService.getRandom(templateId,num,unLabelImageIds));
    }

    @PostMapping("")
    public Result<?> labelImage(@RequestBody LabelImageParam labelImageParam) {
        labelImageService.labelImage(labelImageParam);
        return Result.OK();
    }

    @PutMapping("")
    public Result<?> back(@RequestParam(value = "labelImageIds") List<Long> labelImageIds) {
        for (Long id:labelImageIds)
        {
            labelImageService.lambdaUpdate()
                    .eq(LabelImageEntity::getId,id)
                    .set(LabelImageEntity::getLabelId,null)
                    .update();
        }
        return Result.OK();
    }
}
